using System;
using System.Web.UI;
using System.Collections.ObjectModel;

namespace Controls
{
    /// <summary>
    /// Represents a collection of <see cref="SequenceItem"/> objects for an <see cref="SequenceSplitter"/> control. 
    /// </summary>
    public class SequenceItemCollection : Collection<SequenceItem>
    {
        /// <summary>
        /// Stores value to inidacate if the every <see cref="SequenceItem"/>s
        /// in the collection is initialized.
        /// </summary>
        private bool _initialized;

        /// <summary>
        /// Owner of the current <see cref="SequenceItem"/> collection.
        /// </summary>
        private SequenceSplitter _owner;


        /// <summary>
        /// Creates an instance of <see cref="SequenceSplitter"/>.
        /// </summary>
        /// <param name="owner"></param>
        public SequenceItemCollection(SequenceSplitter owner)
        {
            if (owner == null)
            {
                throw new ArgumentNullException("owner");
            }
            _owner = owner;
        }

        /// <summary>
        /// Removes all the <see cref="SequenceItem"/>s from the current collection.
        /// </summary>
        protected override void ClearItems()
        {
            foreach (SequenceItem eachUpdatePanelLoader in this)
            {
                eachUpdatePanelLoader.SetOwner(null);
            }
            base.ClearItems();
        }

        /// <summary>
        /// Gets if all the <see cref="SequenceItem"/>s in the current collection is loaded.
        /// </summary>
        /// <returns>true, if all the <see cref="SequenceItem"/> are loaded, otherwise false.</returns>
        internal bool HasLoaded()
        {
            foreach (SequenceItem eachUpdatePanelLoader in this)
            {
                if (eachUpdatePanelLoader.HasLoadCompleted())
                {
                    return true;
                }
            }
            return false;
        }

        /// <summary>
        /// Initializes all the <see cref="SequenceItem"/>s in the current collection.
        /// </summary>
        internal void Initialize()
        {
            foreach (SequenceItem eachUpdatePanelLoader in this)
            {
                eachUpdatePanelLoader.Initialize();
            }
            _initialized = true;
        }

        /// <summary>
        /// Inserts a <see cref="SequenceItem"/> into the current collection.
        /// </summary>
        /// <param name="index">Position in the collection to insert the <see cref="SequenceItem"/>.</param>
        /// <param name="item"><see cref="SequenceItem"/> to insert into the collection.</param>
        protected override void InsertItem(int index, SequenceItem item)
        {
            item.SetOwner(Owner);
            if (_initialized)
            {
                item.Initialize();
            }
            base.InsertItem(index, item);
        }

        /// <summary>
        /// Removes a <see cref="SequenceItem"/> from the current collection.
        /// </summary>
        /// <param name="index">Position of the <see cref="SequenceItem"/> in the collection.</param>
        protected override void RemoveItem(int index)
        {
            base[index].SetOwner(null);
            base.RemoveItem(index);
        }

        /// <summary>
        /// Sets a <see cref="SequenceItem"/> item into the current collection.
        /// </summary>
        /// <param name="index">Position in the collection.</param>
        /// <param name="item">A <see cref="SequenceItem"/> to insert into the collection.</param>
        protected override void SetItem(int index, SequenceItem item)
        {
            base[index].SetOwner(null);
            item.SetOwner(Owner);
            if (_initialized)
            {
                item.Initialize();
            }
            base.SetItem(index, item);
        }

        /// <summary>
        /// Owner of the current collection.
        /// </summary>
        public SequenceSplitter Owner
        {
            get
            {
                return _owner;
            }
        }

    }
}